package com.zijie.hosptal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.common.Result;
import com.zijie.hosptal.pojo.po.Doctor;
import com.zijie.hosptal.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/doctor")
public class DoctorController {
    private DoctorService doctorService;

    @Autowired
    public void setDoctorService(DoctorService doctorService) {
        this.doctorService = doctorService;
    }
    @GetMapping
    public Result<?> getAllDoctorList(Integer page,Integer limit,Doctor doctor){
//        引入mybatis的
        Page<Doctor> doctorPage=Page.of(page,limit);
        doctorPage = doctorService.list(doctorPage, doctor);
        return Result.success(doctorPage);
    }
    //添加医生
    @PostMapping
    public Result<?> addDoctor(@RequestBody @Validated  Doctor doctor, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            // 获取第一个错误信息
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return Result.fail(errorMessage);
        }
        boolean flag = doctorService.save(doctor);
        return Result.success(flag);
    }
    //删除医生
    @DeleteMapping("/{id}")
    public Result<?> deleteDoctor(@PathVariable Integer id){
        boolean flag = doctorService.removeById(id);
        return Result.success(flag);
    }
    //修改医生
    @PutMapping
    public Result<?> updateDoctor(@RequestBody @Validated  Doctor doctor, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            // 获取第一个错误信息
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return Result.fail(errorMessage);
        }
        boolean flag = doctorService.updateById(doctor);
        return Result.success(flag);
    }
    //查询全部医生
    @GetMapping("/all")
    public Result<?> getAllDoctorList(){
        List<Doctor> list = doctorService.getAllDoctor();
        return Result.success(list);
    }



}
